package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.Orderdetle;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderDetleDAO {

    public static List<Orderdetle> selectByOid(Integer oid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,b_num bNum from orderdetle where oid = ?";
        try {
            List<Orderdetle> orderdetleList = queryRunner.query(sql,new BeanListHandler<>(Orderdetle.class),oid);
            for(Orderdetle orderdetle : orderdetleList)
            {
                orderdetle.setProduct(ProductDAO.selectById(orderdetle.getPid()));
            }
            return orderdetleList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void insertByOidAndPidAndNum(Integer oid,Integer pid,Integer buyNum)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into orderdetle (oid,pid,b_num) values (?,?,?)";
        try {
            queryRunner.update(sql,oid,pid,buyNum);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
